package com.vast.vastlcd.preferences;

import java.io.File;

import org.eclipse.jface.preference.ListEditor;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;

public class ModuleListEditor extends ListEditor {

	String lastPath;
	
	public ModuleListEditor(String name, String label, String path, Composite parent) {
		super(name,label,parent);
		this.lastPath = path;
	}
	
	@Override
	protected String createList(String[] items) {
		String result = "";
		for (int it=0; it< items.length;it++) {
			result += items[it];
			if (it != (items.length-1))
				result += ";";
		}
		return result;
	}

	@Override
	protected String getNewInputObject() {
		
		FileDialog dialog = new FileDialog(getShell());
		if (System.getProperty("os.name").toLowerCase().contains("win"))
			dialog.setFilterExtensions(new String[]{"*.dll"});
		else
			dialog.setFilterExtensions(new String[]{"*.so"});
		dialog.setText("Select a metrix filter file");
		
		
        if (lastPath != null) {
            if (new File(lastPath).exists()) {
				dialog.setFilterPath(lastPath);
			}
        }
		String file = dialog.open();
        String dir = dialog.getFilterPath();

        if (dir != null) {
            dir = dir.trim();
            if (dir.length() == 0) {
				return null;
			}
            lastPath = dir;
        }
		
		return new File(file).getName();
	}

	@Override
	protected String[] parseString(String stringList) {
		return stringList.split(";");
	}

}
